package cn.edu.cjw.APMFP.ui.panel;


import cn.edu.cjw.APMFP.BOMTable.Service.BTService;
import cn.edu.cjw.APMFP.BOMTable.Service.BTServiceImpl;
import cn.edu.cjw.APMFP.BOMTable.pojo.BOMTable;
import cn.edu.cjw.APMFP.Product.Servicve.ProductService;
import cn.edu.cjw.APMFP.Product.Servicve.ProductServiceImpl;
import cn.edu.cjw.APMFP.Product.pojo.Product;
import cn.edu.cjw.APMFP.Util.StringUtil;
import cn.edu.cjw.APMFP.ui.CenterPanel;
import cn.edu.cjw.APMFP.ui.dialog.BomDialog;
import cn.edu.cjw.APMFP.ui.dialog.ProductDialog;
import cn.edu.cjw.APMFP.ui.table.BomTableModel;
import cn.edu.cjw.APMFP.ui.table.ProductTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @package cn.edu.cjw.APMFP.ui.panel
 * @className ProductPanel
 * @note 商品面板
 * @author wind
 * @date 2022/3/12 22:21
 */
public class ProductPanel extends JPanel {

    private JComboBox<String> typeBox = new JComboBox<>();

    private JTextField searchField = new JTextField(20);

    private JButton searchBtn = new JButton("查询");

    private JButton addBtn = new JButton("新增");

    private JButton editBtn = new JButton("修改");

    private JButton delBtn = new JButton("删除");

    private ProductService service = new ProductServiceImpl();

    private JPanel northPanel = new JPanel();

    private JScrollPane centerPanel = new JScrollPane();

    private JTable table = new JTable();

    private List<Product> list = new ArrayList<>();

    private CenterPanel mainPanel;

    public ProductPanel(CenterPanel mainPanel){
        this.mainPanel = mainPanel;
        init();
    }

    private void init(){
        this.setLayout(new BorderLayout());

        typeBox.addItem("按编号");
        typeBox.addItem("按名称");
        typeBox.setSelectedIndex(0);

        northPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        northPanel.add(searchField);
        northPanel.add(searchBtn);
        northPanel.add(addBtn);
        northPanel.add(editBtn);
        northPanel.add(delBtn);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getTableHeader().setReorderingAllowed(false);
        initTable();
        centerPanel.setViewportView(table);

        searchBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                initTable();
            }
        });

        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ProductDialog(null, () -> {
                    initTable();
                });
            }
        });

        editBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                if(row == -1){
                    JOptionPane.showMessageDialog(null, "请选择修改行");
                    return;
                }
                new ProductDialog(list.get(row), () -> {
                    initTable();
                });
            }
        });

        delBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = table.getSelectedRow();
                if(row == -1){
                    JOptionPane.showMessageDialog(null, "请选择删除行");
                    return;
                }
                try {
                    service.removeProduct(list.get(row));
                    initTable();
                    JOptionPane.showMessageDialog(null, "删除成功");
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "删除失败:" + ex.getMessage());
                }
            }
        });

        this.add(northPanel, BorderLayout.NORTH);
        this.add(centerPanel, BorderLayout.CENTER);
    }

    public void initTable(){
        String searchTxt = searchField.getText().trim();
        this.list.clear();
        try {
            if(StringUtil.isNotBlank(searchTxt)){
                int index = typeBox.getSelectedIndex();
                if(index == 0){
                    this.list.addAll(Collections.singletonList(service.searchProductById(searchTxt)));
                }else{
                    this.list.addAll(service.searchProductByName(searchTxt));
                }
            }else{
                this.list.addAll(service.showAll());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ProductTableModel model = new ProductTableModel(list);
        table.setModel(model);
        model.fireTableDataChanged();
    }
}
